import { request, isPhoneNum } from "../../utils/util";
let app = getApp()

Page({
  data: {
    alreadyTap: false,
    timeDown: 60,
    timer: null,
    form: {
      telephone: '',
      code: ''
    }
  },
  onInputChange(e) {
    const { value } = e.detail;
    const { name } = e.currentTarget.dataset;
    const { form } = this.data;
    this.setData({
      form: { ...form, [name]: value }
    });
  },
  getVerifyCode() {
    if (!this.data.form.telephone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!isPhoneNum(this.data.form.telephone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/sms',
      method: 'POST',
      data: {
        telephone: this.data.form.telephone
      },
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          wx.showToast({
            title: '发送成功！',
            icon: 'success',
            duration: 2000
          })
          this.countDown()
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  countDown() {
    this.setData({
      alreadyTap: true
    });
    this.timer = setInterval(() => {
      this.setData({
        timeDown: --this.data.timeDown
      })
      if (this.data.timeDown === 0) {
        clearInterval(this.timer)
        this.setData({
          alreadyTap: false
        });
      }
    }, 1000)
  },
  save() {
    if (!this.data.form.telephone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!isPhoneNum(this.data.form.telephone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false
    } else if (!this.data.form.code) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    wx.showLoading({
      title: '加载中..',
      mask: true
    })
    request({
      url: '/api/web/bind-phone',
      method: 'POST',
      data: this.data.form,
      success: ({ code, message }) => {
        wx.hideLoading()
        if (code === 200) {
          wx.showToast({
            title: '登录成功！',
            icon: 'success',
            duration: 2000
          })
          app.globalData.userInfo.telephone = this.data.form.telephone
          wx.setStorage({
            key: "userInfo",
            data: app.globalData.userInfo
          })
          setTimeout(() => {
            wx.navigateBack()
          }, 1000)
        } else {
          wx.showToast({
            title: message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  onShow () {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: '#000000',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },
})
